package com.turing.post.report.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.report.util.SaleCapitalDaySettlement;
import com.turing.post.report.util.SaleCapitalDaySettlementDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "report.impl.SaleCapitalDaySettlementDaoImpl")
@Remote
@Local
public class SaleCapitalDaySettlementDaoImpl implements
		SaleCapitalDaySettlementDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	public Date getMaxTranDate(Date tranDate, String saleBranchNo) {
		Query query = _em
				.createNativeQuery("select Max(t.tranDate) from SaleCapitalDaySettlement t "
						+ "where t.saleBranchNo = ?1 and t.tranDate < ?2");
		query.setParameter(1, saleBranchNo);
		query.setParameter(2, tranDate);

		return (Date) query.getSingleResult();
	}

	public SaleCapitalDaySettlement getSaleCapitalDaySettlementByKey(
			SaleCapitalDaySettlement scds) {
		return _em.find(SaleCapitalDaySettlement.class, scds);
	}

	public void insert(SaleCapitalDaySettlement scds)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, scds);
	}

	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void removeAllSaleCapitalDaySettlementByTranDate(
			String issueCompanyNo, Date tranDate) {
		Query query = _em
				.createNativeQuery("delete from SaleCapitalDaySettlement t "
						+ "where t.saleBranchNo like ?1 and t.tranDate = ?2");
		query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, tranDate);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getPrepaymentBalanceReport(PCPSWebInput webInput,
			String startTranDate, String endTranDate) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String sqlTranDate = "";
		if (!PCPSUtil.isEmpty(startTranDate))
			sqlTranDate += " and (t.trandate is null or t.trandate>=to_date('"
					+ startTranDate + "','yyyy-MM-dd')) ";
		if (!PCPSUtil.isEmpty(endTranDate)) {
			sqlTranDate += " and (t.trandate is null or t.trandate<=to_date('"
					+ endTranDate + "','yyyy-MM-dd')) ";
		}
		String sql = "select t.trandate,t.merchantno,t.consume-t.deposit-t.cancelpaid,t.consumecount+t.depositcount+t.cancelpaidcount,0 scoreconsume,0 scorecount,t.paidfund,t.consume,t.consumecount+t.depositcount+t.cancelpaidcount from merchantdaysettlement t where t.issuecompanyno='"
				+ PCPSUtil.getIssueCompanyNo(webInput)
				+ "' "
				+ sqlTranDate + " order by t.merchantno,t.trandate";
		Query query = _em.createNativeQuery(sql);
		List<Object[]> list = query.getResultList();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getRegionReserveBalanceChangeReport(
			PCPSWebInput webInput, String startTranDate, String endTranDate) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String sqlTranDate = "";
		if (null != startTranDate)
			sqlTranDate += " and (s.trandate is null or s.trandate>=to_date('"
					+ startTranDate + " 00:00:00','yyyy-MM-dd hh24:mi:ss')) ";
		if (null != endTranDate) {
			sqlTranDate += " and (s.trandate is null or s.trandate<=to_date('"
					+ endTranDate + " 23:59:59','yyyy-MM-dd hh24:mi:ss')) ";
		}
		String sql = "select  s.salebranchno,sum(s.LastCredit),sum(s.CreditDeposit),sum(s.CreditWithdraw),sum(s.Credit) from salecapitaldaysettlement s where 1 = 1"
				+ sqlTranDate
				+ " group by s.salebranchno order by s.salebranchno";
		Query query = _em.createNativeQuery(sql);
		List<Object[]> list = query.getResultList();
		return list;
	}
}